"""
Here a list of tools for editing data in table and listviews.
"""

from PyQt4 import QtCore
import numpy as np

reservedNames = ('i','j','k')

class dataSpace(object):
    '''
    A namespace for local data storage.
    '''
    def __init__(self,pubTypes=[]):
        self.__pubKeys = []
        self.__pubTypes = pubTypes
    
    def __str__(self):
        return 'dataSpace'
    
    def __getitem__(self,varName):
        return self.__dict__[varName]
    
    def __setitem__(self,varName,item):
        self.__dict__[varName] = item
    
    def setPubTypes(self,pubTypes):
        self.__pubTypes = pubTypes

    def buildPubKeys(self):
        self.__pubKeys = []
        for i in self.__dict__.keys():
            if self.isVarPublic(i):
                self.__pubKeys.append(i)

    def clear(self):
        self.__dict__.clear()
        self.__pubKeys = []
        self.__pubTypes = []
    
    def popItem(self,varName):
        del self.__dict__[varName]
        index = self.__pubKeys.index(varName)
        self.__pubKeys.pop(index)
    
    def merge(self,newDict):
        return self.__dict__.update(newDict)
    
    def getKeys(self):
        return self.__dict__.keys()

    def getPubKeys(self):
        return self.__pubKeys
    
    def getPubDict(self):
        pubDict = {}
        for i in self.__dict__.iterkeys():
            if self.isVarPublic(i):
                pubDict[i] = self.__dict__[i]
        return pubDict
    
    def getLen(self):
        return len(self.__dict__)
    
    def getPubLen(self):
        return len(self.__pubKeys)
    
    def isVarPublic(self,varName):
        if type(self.__dict__[varName]) \
            in self.__pubTypes \
            and varName[0] is not '_' \
            and varName not in reservedNames:
            return True
        return False

class localDataModel(QtCore.QAbstractListModel):
    """
    Concrete model for showing and editing localy
    stored variables.
    """
    def __init__(self,localVars={},parent=None):
        self.__theLocalVars = localVars
        self.__theLocalVars.buildPubKeys()
        return QtCore.QAbstractListModel.\
            __init__(self,parent)
    
    def reload(self):
        self.__theLocalVars.buildPubKeys()
        return self.reset()
    
    def setLocalVars(self,localVars):
        self.__theLocalVars = localVars
        self.__theLocalVars.buildPubKeys()
        return self.reset()
        
    def rowCount(self,index):
        return self.__theLocalVars.getPubLen()

    def data(self,index,role):
        if index.isValid() and \
        index.row() < self.rowCount(index):
            if role == QtCore.Qt.DisplayRole:
                return self.__getValidEntry(index)
            elif role == QtCore.Qt.ToolTipRole:
                return self.__getValidInfo(index)
        return QtCore.QVariant()
    
    def __getValidEntry(self,index):
        varKey = self.__theLocalVars.getPubKeys()[index.row()]
        varType = str(type(self.__theLocalVars[varKey]))
        varString = QtCore.QString(varKey+': '+varType)
        return QtCore.QVariant(varString)

    def __getValidInfo(self,index):
        varKey = self.__theLocalVars.getPubKeys()[index.row()]
        varShape = str(np.shape(self.__theLocalVars[varKey]))
        varType = str(type(self.__theLocalVars[varKey]))
        varString = QtCore.QString("type: %s; shape: %s" % (varType,varShape))
        return QtCore.QVariant(varString)
        